home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pas_0593.zip / 3DEQU.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-30  |  3KB  |  92 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 159 of 286
  3. From : Steve Connet                        1:300/15.0           11 May 93  11:16
  4. To   : Tony Wouters
  5. Subj : Pascal 3D equations
  6. ────────────────────────────────────────────────────────────────────────────────
  7. This message was from SCOTT CAMPBELL to JAMES MASTERS
  8. originally in conference PASCAL langua
  9. and was forwarded to you by STEVE CONNET
  10.                     ----------------------------------------
  11. Okay, here's the equations for 3D rotations...
  12.  
  13. x,y,z are the coordinates of the point you want to rotate.
  14. rx,ry,rz are the amount of rotation you want (in degrees) for x,y,z}
  15.  
  16.   x1:=round(cos(rad(ry))*x-sin(rad(ry))*z); 
  17.   z1:=round(sin(rad(ry))*x+cos(rad(ry))*z); 
  18.   x:=round(cos(rad(rz))*x1+sin(rad(rz))*y); 
  19.   y1:=round(cos(rad(rz))*y-sin(rad(rz))*x1); 
  20.   z:=round(cos(rad(rx))*z1-sin(rad(rx))*y1); 
  21.   y:=round(sin(rad(rx))*z1+cos(rad(rx))*y1); 
  22.  
  23. {
  24. Because in Turbo Pascal, COS and SIN require radians for the argument,
  25. I wrote a short function called RAD() that converts degrees into radians
  26. (I find degrees much easier to visualize) }
  27.  
  28.  
  29.   function rad(i:integer):real;
  30.   begin
  31.     rad:=i*(pi/360);
  32.   end;
  33.  
  34. {
  35. Of course, since most computers don't have 3D projection screens <G>,
  36. use these equations to provide a sense of perspective to the object,
  37. but with 2D coordinates you can plot on a screen.
  38.  
  39. x,y,z are from the equations above, and xc,yc,zc are the center points
  40. for the object that you are rotating... I recommend setting xc,yc at 0,0
  41. but zc should be very high (+100). }
  42.  
  43.   x2:=trunc((xc*z-x*zc)/(z-zc));
  44.   y2:=trunc((yc*z-y*zc)/(z-zc));
  45.  
  46. {
  47. Alternatively, if you don't want to bother with perspective, just drop
  48. the z values, and just plot the (x,y) instead.
  49.  
  50.  
  51. To use these equations, pick a 3D object and figure out what the 3D
  52. coordinates are for each point on the object.  You will have to have some
  53. way to let the computer know which two points are connected.  For the
  54. cube that I did, I had one array for the points and one for each face
  55. of the cube.  That way the computer can draw connecting lines for each
  56. face with a simple for-loop. }
  57.  
  58. type
  59.  FACELOC=array[1..4] of integer
  60.  POINTLOC=record;
  61.    x,y,z:integer;
  62.     end;
  63.  
  64. const
  65. face_c:array [1..6] of faceloc =(
  66.    (1,2,3,4),
  67.    (5,6,2,1),
  68.    (6,5,8,7),
  69.    (4,3,7,8), 
  70.    (2,6,7,3), 
  71.    (5,1,4,8)); 
  72.  
  73.  point_c:array [1..8] of pointloc =( 
  74.    (-25, 25, 25), 
  75.    ( 25, 25, 25), 
  76.    ( 25,-25, 25), 
  77.    (-25,-25, 25), 
  78.    (-25, 25,-25),
  79.    ( 25, 25,-25),
  80.    ( 25,-25,-25),
  81.    (-25,-25,-25));
  82. {
  83. There you go.  I'm not going to get much more complicated for now.  If you
  84. can actually get these equations/numbers to work (and I haven't forgotten 
  85. anything!) leave me another message, and I'll give you some advice for 
  86. filling in the sides of the object (so that you can only see 3 sides at 
  87. once) and some advice to speed things up abit.  If you have any problems 
  88. with whats here, show some other people, and maybe as a collective you can 
  89. figure it out.  Thats how I got this one started! 
  90.  
  91. Send me a message sometime and let me know if I did forget something....}
  92.